package com.tem.payment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.payment.entity.PayJob;
import com.api.manage.pojo.payment.PayJobBean;
import com.tem.payment.service.IPayJobService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 缓存到mongoDB 前端控制器
 * </p>
 * @author 鸿源塔基
 * @since 2023-10-27
 */
@RestController
@RequestMapping("/payment/payJob")
public class PayJobController extends BaseController{
    @Autowired
    private IPayJobService payJobService;

    @ApiOperation(value = "缓存到mongoDB列表")
    @RequestMapping("list")
    public ResultUtil<IPage<PayJobBean>> list(PayJobBean request){
        QueryWrapper<PayJob> wrapper = new QueryWrapper<>();
        if(null != request.getStartTime() && null != request.getEndTime()) {
            wrapper.between("create_time",DateFormatUtils.format(request.getStartTime(),"yyyy-MM-dd HH:mm:ss"),DateFormatUtils.format(request.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
        }
        wrapper.orderByDesc("pay_job_id");
        Page page = new Page<>(request.getCurrent(),request.getSize());
        IPage listBody = payJobService.page(page,wrapper);
        List<PayJob> entityList=listBody.getRecords();
        if(!entityList.isEmpty()) {
            List<PayJobBean> responseList = new ArrayList<>();
            entityList.forEach(it -> {
                PayJobBean bean = new PayJobBean();
                BeanUtils.copyProperties(it,bean);
                responseList.add(bean);
            });
            listBody.setRecords(responseList);
        }
        return ResultUtil.success(listBody);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增缓存到mongoDB")
    public ResultUtil save(@RequestBody PayJob entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getPayJobId()!=null){
            payJobService.updateById(entity);
        }else{
            payJobService.save(entity);
        }
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询缓存到mongoDB")
    public ResultUtil<PayJobBean> getById(@PathVariable String id){
        PayJob entity = payJobService.getById(id);
        if(entity!=null){
            PayJobBean bean = new PayJobBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改缓存到mongoDB")
    public ResultUtil update(@RequestBody PayJob entity){
        payJobService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除缓存到mongoDB")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        payJobService.removeById(id);
        return ResultUtil.success();
    }
}